﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.Services;
using RawInput_dll;

namespace ComputerVision
{
    public partial class frmSaoLuu : frmObject
    {
        private string connectionstring = null;
        private SqlConnectionStringBuilder builder;
        SqlConnection connection = null;

        public frmSaoLuu()
        {
            InitializeComponent();
            connectionstring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            builder = new SqlConnectionStringBuilder(connectionstring);
        }

        private void btSaoLuu_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "Backup_NMSP_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".bak";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                string sql = "Backup database ComputerVision to disk='" + saveFileDialog1.FileName + "'";
                if (ExecuteNonQuery(sql) > 0)
                {
                    Cursor.Current = Cursors.Default;
                    AlertList.Information("Sao lưu dữ liệu thành công");
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    AlertList.Error("Sao lưu dữ liệu thất bại. Vui lòng thử lại sau");
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu xe vào ra? \nHãy cân nhắc trước khi đồng ý.", @"Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                Cursor.Current = Cursors.WaitCursor;
                string sql = "DELETE FROM [XeTemp]; DELETE FROM [Xe]; ";
                if (ExecuteNonQuery(sql) > 0)
                {
                    Cursor.Current = Cursors.Default;
                    AlertList.Information("Xóa dữ liệu thành công");
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    AlertList.Error("Gặp lỗi khi xóa dữ liệu. Xin vui lòng thực hiện thao tác này sau.");
                }
            }
        }

        public int ExecuteNonQuery(string inputQuery)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = inputQuery;
            command.CommandTimeout = 0;
            int result = 0;
            try
            {
                Connect(connectionstring,out connection);
                command.Connection = connection;
                command.ExecuteNonQuery();
                result = 1;
                return result;

            }
            catch (Exception e)
            {
                MessageBox.Show("Error:" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return result;
            }
            finally
            {
                if (connection != null && connection.State != ConnectionState.Closed)
                    connection.Close();
            }
        }

        private void Connect(string connect, out SqlConnection connection)
        {
            //connection.Close();
            //string connectString = "server=.;uid=sa; pwd=123456; database=rrc_database;";	
            connection = new SqlConnection(connect);
            try
            {
                connection.Open();
                //return connection;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error:" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //return null;

            }
        }

        private void btKhoiPhuc_Click(object sender, EventArgs e)
        {
            //openFileDialog1.FileName = "Backup_NMSP_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".bak";
            connectionstring = "data source=" + builder.DataSource + ";User ID=" + builder.UserID + ";password=" + builder.Password + ";initial catalog=master;";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                string sql = "DROP DATABASE ComputerVision; RESTORE DATABASE ComputerVision FROM DISK = N'" + openFileDialog1.FileName + "' WITH  FILE = 1,  NOUNLOAD,  STATS = 10";
                if (ExecuteNonQuery(sql) > 0)
                {
                    Cursor.Current = Cursors.Default;
                    AlertList.Information("Khôi phục dữ liệu thành công");
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    AlertList.Error("Khôi phục dữ liệu thất bại. Vui lòng thử lại sau");
                }
            }
        }


    }
}